package com.aqie.security.demo.service;

import com.aqie.security.demo.dto.User;
import com.aqie.security.demo.dto.condition.UserQueryCondition;
import com.aqie.security.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

/**
 * @Function:
 * @Author: aqie
 * @Date: 2019/4/3 13:35
 */
@Service
public class UserServiceImpl {
    @Autowired
    private UserRepository userRepository;

    public User createOrUpdate(User user){
        return userRepository.save(user);
    }

    public User findUserByUsername(String username){
        return userRepository.findByUsername(username);
    }
    public User select(int id){
        return userRepository.getOne(id);
    }

    public void delete(int id){
        userRepository.deleteById(id);
    }

    public Page<User> selectAll(UserQueryCondition condition){
        Sort sort = new Sort(Direction.DESC, "id");
        Pageable pageable = PageRequest.of(condition.getPage() - 1,condition.getLength(), sort);
        return userRepository.findAll(pageable);
    }
}
